package YZ.Students.Mier.Dao.impl;

import YZ.Students.Mier.Dao.InvitataionDao;
import YZ.Students.Mier.Tool.utils.druidTool;

import YZ.Students.Mier.entity.InvitataionEntity;
import YZ.Students.Mier.entity.UserEntity;
import YZ.Students.Mier.vo.IndexDataVo;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class InvitataionDaoImpl implements InvitataionDao {

    private QueryRunner queryRunner = new QueryRunner(druidTool.dataSource);

    @Override
    public Integer insert(InvitataionEntity in) throws Exception {
        String sql = "insert into t_discuss_post(userId,title,content,type,status,createTime,commentCount,score) values(?,?,?,?,?,?,?,?)";
        return queryRunner.update(sql, in.getUserId(), in.getTitle(), in.getContent(), in.getType(), in.getStatus(), in.getCreateTime(), in.getCommentCount(), in.getScore());
    }

    @Override
    public InvitataionEntity selectById(Integer id) throws Exception {
        String sql = "select d.*,d.userId as userId,d.createTime as createTime from t_discuss_post d where id = ?";
        return queryRunner.query(sql, new BeanHandler<InvitataionEntity>(InvitataionEntity.class), id);
    }

    @Override
    public Integer updateById(InvitataionEntity invitataionEntity) throws Exception {
        return null;
    }

    @Override
    public Integer deleteById(Integer id) throws Exception {
        return null;
    }

    @Override
    public List<InvitataionEntity> selectList() throws Exception {
        return null;
    }

    @Override
    public Integer batchRemove(List<Integer> ids) throws Exception {
        return null;
    }

    @Override
    public List<IndexDataVo> indexDataList() throws Exception {
        String sql = "SELECT d.id, d.userId AS userId, d.title, d.content, d.type, d. STATUS, d.createTime, d.score, u.username, u.headerUrl FROM t_discuss_post d LEFT JOIN t_user u ON (u.id = d.userId)";
        List<IndexDataVo> list = queryRunner.query(sql, new BeanListHandler<IndexDataVo>(IndexDataVo.class) {
            @Override
            public List<IndexDataVo> handle(ResultSet resultSet) throws SQLException {

                // 1.准备一个集合，用来装查询的出来的数据
                List<IndexDataVo> indexDataVoList = new ArrayList<>();
                while (resultSet.next()) {

                    // 2.创建一个IndexDataVo(帖子信息，作者信息)
                    IndexDataVo indexDataVo = new IndexDataVo();

                    // 3.创建一个帖子和作者的对象对象
                    InvitataionEntity invitataionEntity = new InvitataionEntity();
                    UserEntity userEntity = new UserEntity();

                    // 4.从结果集中把帖子和作者的数据取出来，放到对应的两个对象中
                    int id = resultSet.getInt("id");
                    int userId = resultSet.getInt("userId");
                    String title = resultSet.getString("title");
                    String content = resultSet.getString("content");
                    int type = resultSet.getInt("type");
                    int status = resultSet.getInt("STATUS");
                    Date createTime = resultSet.getDate("createTime");
                    String username = resultSet.getString("username");
                    String headerUrl = resultSet.getString("headerUrl");

                    // 封装帖子信息
                    invitataionEntity.setId(id);
                    invitataionEntity.setUserId(userId);
                    invitataionEntity.setContent(content);
                    invitataionEntity.setType(type);
                    invitataionEntity.setStatus(status);
                    invitataionEntity.setTitle(title);
                    invitataionEntity.setCreateTime(createTime);

                    // 封装作者的信息
                    userEntity.setUsername(username);
                    userEntity.setHeaderUrl(headerUrl);

                    // 5.把两个对象放在indexDataVo
                    indexDataVo.setUserEntity(userEntity);
                    indexDataVo.setInvitataionEntity(invitataionEntity);

                    // 6.把indexDataVo放到集合中
                    indexDataVoList.add(indexDataVo);
                }
                return indexDataVoList;
            }
        });
        return list;
    }
}
